package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.ChatRecordDao;
import cn.gson.springboot.model.dao.TaskDao;
import cn.gson.springboot.model.mapper.TaskMapper;
import cn.gson.springboot.model.pojos.ChatRecord;
import cn.gson.springboot.model.pojos.Task;
import cn.gson.springboot.model.vo.ChatRecordVo;
import cn.gson.springboot.model.vo.TaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class TaskService {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    TaskDao taskDao;
    @Autowired
    ChatRecordDao chatDao;
    public List<Task> selectTaskAction(Integer deptId,Integer empId,Integer value,Integer num,String content){
        return taskMapper.selectTaskAction(deptId,empId,value,num,content);
    }

    public List<Task> selectTaskActionByCusName(Integer type,Integer cusId){
        return taskMapper.selectTaskActionByCusName(type,cusId);
    }

    public List<Task> selectTaskByOppId(Integer oppId){
        return taskMapper.selectTaskByOppId(oppId);
    }

    public List<Task> selectTaskActionByOppId(Integer oppId){
        return taskMapper.selectTaskActionByOppId(oppId);
    }

    public List<Map<String,Object>> computeActionTask(Integer deptId,Integer empId){
        return taskMapper.computeActionTask(deptId,empId);
    }

    public List<Map<String,Object>> computeActionClassify(Integer deptId,Integer empId){
        return taskMapper.computeActionClassify(deptId,empId);
    }

    public List<Map<String,Object>> computeTaskStatus(Integer deptId,Integer empId){
        return taskMapper.computeTaskStatus(deptId,empId);
    }

    public List<Map<String,Object>> computeTaskPriority(Integer deptId,Integer empId){
        return taskMapper.computeTaskPriority(deptId,empId);
    }
    public List<Map<String,Object>> computeNumAndDate(Integer deptId,Integer empId){
        return taskMapper.computeNumAndDate(deptId,empId);
    }

    public List<Map<String,Object>> computeCusNum(Integer deptId,Integer empId){
        return taskMapper.computeCusNum(deptId,empId);
    }

    public List<Map<String,Object>> computeTaskNumAndDate(Integer deptId,Integer empId){
        return taskMapper.computeTaskNumAndDate(deptId,empId);
    }
    public List<Map<String,Object>> computeActionNumAndDate(Integer deptId,Integer empId){
        return taskMapper.computeActionNumAndDate(deptId,empId);
    }
    public List<Task> selectEmpTaskByStatus(Integer empId,Integer taskStatus,Integer expired){
        return taskMapper.selectEmpTaskByStatus(empId,taskStatus,expired);
    }

    public List<Task> selectEmpTaskByChat(Integer empId){
        return taskMapper.selectEmpTaskByChat(empId);
    }

    public Task selectTaskByTaskId(Integer taskId){
        return taskMapper.selectTaskByTaskId(taskId);
    }

    @Transactional(transactionManager = "transactionManager")
    public void saveTask(TaskVo vo){
        Task task = new Task();
        task.setTaskId(vo.getTaskId());
        task.setTaskDescribe(vo.getTaskDescribe());
        task.setEmployeeByExeEmpId(vo.getEmployeeByExeEmpId());
        task.setTaskContactName(vo.getTaskContactName());
        task.setTaskFinishDate(vo.getTaskFinishDate());
        task.setTaskTime(vo.getTaskTime());
        task.setTaskStatus(vo.getTaskStatus());
        task.setTaskPriority(vo.getTaskPriority());
        task.setTaskCreateDate(vo.getTaskCreateDate());
        task.setTaskIs(vo.getTaskIs());
        task.setOpportunityByOppId(vo.getOpportunityByOppId());
        task.setCustomerByCustomerId(vo.getCustomerByCustomerId());
        task.setEmployeeByEmpId(vo.getEmployeeByEmpId());
        taskDao.save(task);
        System.out.println("主表ID"+task.getTaskId());
        if (vo.getTaskId() == 0){
            List<ChatRecordVo> chat = vo.getChatRecordsByQuotationId();
            for (ChatRecordVo c : chat) {
                ChatRecord ch = new ChatRecord();
                ch.setChatRecordId(c.getChatRecordId());
                ch.setChatRecordContent(c.getChatRecordContent());
                ch.setChatRecordDate(new Timestamp(System.currentTimeMillis()));
                ch.setTaskByTaskId(task);
                ch.setEmployeeByEmpId(vo.getEmployeeByEmpId());
                chatDao.save(ch);
            }
        }
    }

    @Transactional(transactionManager = "transactionManager")
    public void saveAction(TaskVo vo){
        Task task = new Task();
        task.setTaskId(vo.getTaskId());
        task.setTaskDescribe(vo.getTaskDescribe());
        task.setEmployeeByExeEmpId(vo.getEmployeeByExeEmpId());
        task.setTaskContactName(vo.getTaskContactName());
        task.setTaskFinishDate(vo.getTaskFinishDate());
        task.setTaskClassify(vo.getTaskClassify());
        task.setTaskIs(vo.getTaskIs());
        task.setOpportunityByOppId(vo.getOpportunityByOppId());
        task.setMaintenanceByMaiId(vo.getMaintenanceByMaiId());
        task.setCustomerByCustomerId(vo.getCustomerByCustomerId());
        task.setEmployeeByEmpId(vo.getEmployeeByEmpId());
        taskDao.save(task);
    }

    @Transactional(transactionManager = "transactionManager")
    public void deleteTask(Integer taskId){
        taskDao.deleteById(taskId);
    }
}
